# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json

from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.requestFieldMapDBField import requestFieldMapToDBField


class AdminDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertAds(self, info):
        """
        添加管理员信息
        :param info: 信息字典
        :return:
        """
        roleId = info['roleId']
        roleType = info['roleType']
        adminName = info['adminName']
        adminPassword = info['adminPassword']
        adminSalt = info['adminSalt']
        adminDesc = info['adminDesc']
        adminRealName = info['adminRealName']
        adminTelephone = info['adminTelephone']
        adminEmail = info['adminEmail']
        adminIsLock = info['adminIsLock']

        sql = "insert into zzh_admin (role_id,role_type,admin_name,admin_password,admin_salt,admin_desc," \
              "admin_real_name,admin_telephone,admin_email,admin_is_lock) VALUES " \
              "(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [roleId, roleType, adminName, adminPassword, adminSalt, adminDesc, adminRealName,
                               adminTelephone,
                               adminEmail, adminIsLock])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findAdminById(self, adminId):
        """
        根据ID查询信息
        :param adminId:
        :return:
        """
        sql = "select * from zzh_admin WHERE admin_id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [adminId])
        if rs:
            adinfo = cur.fetchall()
            adJson = Table2Json.admin_list_to_json(adinfo)
            return adJson
        else:
            return None

    def findAdminByCondition(self, **kwargs):

        from com.zzh.utils.jsonUtils_v2.table2Json_Chen import Table2Json
        """
        admin 表条件查询
        :param adminId:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        # 获取对应的映射字典
        fieldMap = requestFieldMapToDBField.adminFieldMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT zzh_admin.*, zzh_admin_role.role_name FROM zzh_admin,zzh_admin_role WHERE " + whereSentence + " and zzh_admin.role_id=zzh_admin_role.role_id ORDER BY " + sortRule
        else:
            sql = "SELECT zzh_admin.*, zzh_admin_role.role_name FROM zzh_admin,zzh_admin_role WHERE " + whereSentence + " and zzh_admin.role_id=zzh_admin_role.role_id "
        rs = cur.execute(sql)

        if rs:
            adinfo = cur.fetchall()
            lists = MyPaging().getPage(rs, pageIndex, pageSize, adinfo)
            adJson = Table2Json.admin_list_to_json(lists)
            return adJson, rs
        else:
            return None, 0

    def deleteAdmin(self, adminIdArray):
        """
        根据ID删除数据表信息，ID需要以数组形式传递
        :param adminIdArray: id 构成的数组
        :return:
        """
        sql = "delete from zzh_admin WHERE admin_id=%s"
        cur = self.conn.cursor()
        rs = 0
        for idd in adminIdArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateAdmin(self, info):
        """
        根据ID更新admin信息
        :param info:
        :return:
        """
        adminId = info['adminId']
        roleId = info['roleId']
        roleType = info['roleType']
        adminName = info['adminName']
        adminPassword = info['adminPassword']
        adminSalt = info['adminSalt']
        adminDesc = info['adminDesc']
        adminRealName = info['adminRealName']
        adminTelephone = info['adminTelephone']
        adminEmail = info['adminEmail']
        adminIsLock = info['adminIsLock']

        sql = "update zzh_admin set role_id=%s, role_type=%s,admin_name=%s,admin_password=%s,admin_salt=%s,admin_desc=%s," \
              "admin_real_name=%s,admin_telephone=%s,admin_email=%s,admin_is_lock=%s " \
              " WHERE admin_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [roleId, roleType, adminName, adminPassword, adminSalt, adminDesc, adminRealName,
                               adminTelephone,
                               adminEmail, adminIsLock, adminId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectAdmin(self, adminId):
        """
        根据ID查询信息，返回元组
        :param adminId:
        :return: data tuple
        """
        sql = "select * from zzh_admin WHERE admin_id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [adminId])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr

    def admin_login(self, loginData):
        """
        login
        :param loginData:
        :return:
        """
        sql = "select admin_password, admin_is_lock from zzh_admin WHERE admin_name=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [loginData['admin_name']])
        if rs:
            data = cur.fetchone()
            isLock = data[1]
            if isLock == 0:
                return 0, None
            elif loginData['admin_password'] == data[0]:
                sql0 = "select admin_id,role_id,admin_name,admin_real_name from zzh_admin WHERE admin_name=%s limit 1"
                rs = cur.execute(sql0, [loginData['admin_name']])
                list0 = cur.fetchone()
                info = [{"adminId": list0[0], "roleId": list0[1], "adminName": list0[2], "adminRealName": list0[3]}]
                return 1, info
            else:
                return -2, None

        else:
            return -1, None
